<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    // 防抖 Debounce
// 定义：当事件持续触发时，延迟执行回调函数，若在等待期内再次触发事件，则重新计时
function Debounce(fn, delay) {
   let timer = null;
   return function(){
    let context = this;
     clearTimeout(timer);
     timer = setTimeout(() => {
       fn.apply(context, arguments);
     }, delay);
   }
}

// 监听按钮click
document.getElementById('btn').addEventListener('click', Debounce(function () {
  console.log('防抖');
}, 1000));



// let timer = null;
// return function () {
//   let context = this;
//   let args = arguments;
//   clearTimeout(timer);
//   timer = setTimeout(function () {
//     fn.apply(context, args);
//   }, delay);
// };
</body>
</html>